package com.gy.controller.notice;



import com.gy.service.NoticeService;
import com.gy.service.impl.CacheUpdateService;
import com.gy.vo.NoticeVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import java.util.List;

@RestController
@Slf4j
@RequestMapping("/notice")
@Api(tags = "公告相关接口")
@CrossOrigin
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Resource
    private CacheUpdateService cacheUpdateService;

    /**
     * 查询最新的三条公告
     */
    @GetMapping("/latest")
    @ApiOperation("获取最新的三条公告")
    @Cacheable(value = "latestNotices", key = "'latest'", sync = true)
    public List<NoticeVO> getLatestNotices() {
        // 如果缓存已经失效或即将过期，调用异步刷新缓存
        cacheUpdateService.refreshCacheAsync();
        // 从数据库获取最新数据
        return noticeService.getLatestNotices();
    }
}
